package iweb2.ch3.collaborative.similarity;

import iweb2.ch3.collaborative.model.Dataset;
import iweb2.ch3.collaborative.model.Item;

/**
 * 
 * @author lei.chenglei
 * 
 */
public class ImprovedItemBasedSimilarity extends BaseSimilarityMatrix {

	private static final long serialVersionUID = -8364129617679022295L;

	public ImprovedItemBasedSimilarity(String id, Dataset dataSet,
			boolean keepRatingCountMatrix) {
		this.id = id;
		this.keepRatingCountMatrix = keepRatingCountMatrix;
		this.useObjIdToIndexMapping = dataSet.isIdMappingRequired();
		calculate(dataSet);
	}

	@Override
	protected void calculate(Dataset dataSet) {

		int nItems = dataSet.getItemCount();
		int nRatingValues = 5;
		similarityValues = new double[nItems][nItems];

		if (useObjIdToIndexMapping) {
			for (Item item : dataSet.getItems()) {
				idMapping.getIndex(String.valueOf(item.getId()));
			}
		}

		if (keepRatingCountMatrix) {
			ratingCountMatrix = new RatingCountMatrix[nItems][nItems];
		}

		for (int u = 0; u < nItems; u++) {

			int itemAId = getObjIdFromIndex(u);
			Item itemA = dataSet.getItem(itemAId);

			for (int v = u + 1; v < nItems; v++) {

				int itemBId = getObjIdFromIndex(v);
				Item itemB = dataSet.getItem(itemBId);

				RatingCountMatrix rcm = new RatingCountMatrix(itemA, itemB,
						nRatingValues);

				int totalCount = rcm.getTotalCount();
				int agreementCount = rcm.getAgreementCount();

				if (agreementCount > 0) {

					double weightedDisagreements = 0.0;
					int maxBandId = rcm.getMatrix().length - 1;
					for (int matrixBandId = 1; matrixBandId <= maxBandId; matrixBandId++) {
						double bandWeight = matrixBandId;
						weightedDisagreements += bandWeight
								* rcm.getBandCount(matrixBandId);
					}

					double similarityValue = 1.0 - (weightedDisagreements / totalCount);

					double normalizedSimilarityValue = (similarityValue - 1.0 + maxBandId)
							/ maxBandId;
					similarityValues[u][v] = normalizedSimilarityValue;
				} else {
					similarityValues[u][v] = 0.0;
				}

				if (keepRatingCountMatrix) {
					ratingCountMatrix[u][v] = rcm;
				}
			}
			similarityValues[u][u] = 1.0;
		}
	}
}
